package com.meixiang.adapter.shopping;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.TransportEntity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;
import com.meixiang.view.LineView;

import java.util.ArrayList;

/**
 * 配送方式列表适配器
 *
 * @author LZRUI
 */
public class DeliveryMethodAdapter extends RecyclerView.Adapter<DeliveryMethodAdapter.MethodViewHolder> {

    private ArrayList<TransportEntity> methodList;
    private OnItemClick onItemClick;
    private Context mContext;

    public DeliveryMethodAdapter(Context context, ArrayList<TransportEntity> list, OnItemClick onItemClick) {
        this.methodList = list;
        this.onItemClick = onItemClick;
        this.mContext = context;
    }

    @Override
    public MethodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_delivery_method, parent, false);
        return new MethodViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MethodViewHolder holder, int position) {
        TransportEntity entity = methodList.get(position);
        if (entity == null) return;
        if(entity.getTransportPrice().trim().equals("0") || entity.getTransportPrice().trim().equals("0.00")){
            holder.tvMethod.setText(entity.getTransportName());
        }else {
            holder.tvMethod.setText(String.format(mContext.getString(R.string.list_goods_transport), entity.getTransportName(), entity.getTransportPrice()));
        }
        if (position == methodList.size() - 1) {
            holder.lineView.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                if (onItemClick != null) {
                    onItemClick.onItemClick(v, holder.getLayoutPosition());
                }
            }
        });

        if (position == methodList.size() - 1) {
            holder.lineView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return methodList == null ? 0 : methodList.size();
    }

    static class MethodViewHolder extends RecyclerView.ViewHolder {

        TextView tvMethod;
        LineView lineView;

        public MethodViewHolder(View itemView) {
            super(itemView);
            tvMethod = (TextView) itemView.findViewById(R.id.tv_method);
            lineView = (LineView) itemView.findViewById(R.id.line);
        }
    }
}
